﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

using Microsoft.SqlServer.Management.Smo;
using Microsoft.Win32;

namespace SQLDataTransferer.ViewModel
{
    public class AttachDatabaseViewModel : SmoViewModelBase
    {
        #region Fields

        OpenFileDialog _fileDialog;

        RelayCommand _openFileDialogCommand;
        RelayCommand _attachCommand;

        #endregion

        #region Constructor(s)

        public AttachDatabaseViewModel(string displayName, System.Globalization.CultureInfo culture, Dispatcher dispatcher, Window main)
            : base(displayName, culture, dispatcher, main)
        {
            this.ShowFileDialog();
        }

        #endregion

        #region Properties

        private OpenFileDialog FileDialog
        {
            get
            {
                if (this._fileDialog == null)
                    this._fileDialog = new OpenFileDialog()
                    {
                        Title = "Select database file",
                        CheckFileExists = true,
                        CheckPathExists = true,
                        DefaultExt = ".mdf",
                        Filter = "Database files (.mdf)|*.mdf",
                        AddExtension = true,
                        Multiselect = false,
                        //TODO: Set directory of sql server files
                        //InitialDirectory = @"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA",
                    };
                return this._fileDialog;
            }
        }

        public FileInfo SelectedDatabaseFile { get; set; }

        #region Commands

        public ICommand OpenFileDialogCommand
        {
            get
            {
                if (this._openFileDialogCommand == null)
                    this._openFileDialogCommand = new RelayCommand((o) => this.ShowFileDialog());

                return this._openFileDialogCommand;
            }
        }

        public ICommand AttachCommand
        {
            get
            {
                if (this._attachCommand == null)
                    this._attachCommand = new RelayCommand((o) => this.OnAttach());

                return this._attachCommand;
            }
        }

        #endregion

        #endregion

        #region Methods

        private void ShowFileDialog()
        {
            bool? dialogResult = this.FileDialog.ShowDialog(this.MainWindow);
            if (!dialogResult.HasValue ||
                !dialogResult.Value)
                return;

            this.SelectedDatabaseFile = new FileInfo(this.FileDialog.FileName);
            this.OnPropertyChanged("SelectedDatabaseFile");
        }

        private void OnAttach()
        {
            this.SetFocus();

            this.RunTaskDialog("Attaching database ...",
                () =>
                {
                    //TODO:

                    StringCollection sc = new StringCollection();
                    // Add database FullPathName
                    sc.Add(this.SelectedDatabaseFile.FullName);

                    string shortdbName = Path.GetFileNameWithoutExtension(this.SelectedDatabaseFile.Name);
                    string logFile = string.Format(@"{0}.ldf", Path.Combine(this.SelectedDatabaseFile.DirectoryName, shortdbName));
                    if (File.Exists(logFile))
                        sc.Add(logFile);

                    Server server;
                    if (this.CurrentOption == RadioButtonOptions.Windows)
                        server = SmoFactory.GetServer(SmoFactory.CreateConnection(this.SelectedServer.FullName));
                    else
                        server = SmoFactory.GetServer(SmoFactory.CreateConnection(this.SelectedServer.FullName, "TODO:", "TODO:"));

                    // Attach the database to the server
                    SmoFactory.AttachDatabase(server, shortdbName, sc, null);

                    //TODO:

                    // Clear the file to attach
                    this.SelectedDatabaseFile = null;
                    this.OnPropertyChanged("SelectedDatabaseFile");
                });
        }

        #endregion
    }
}
